﻿Imports System.Windows.Forms
Imports System.Data.SqlClient

Public Class frmGetLookupTableSettings
    Public TableName As String = ""
    Public DisplayColumn As String = ""
    Public ReturnColumn As String = ""
    Public OkClicked As Boolean = False

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim DataValidated As Boolean = False
        If Len(cboTableName.Text) > 0 Then
            If Len(cboDisplayColumn.Text) > 0 Then
                If Len(cboReturnColumn.Text) > 0 Then
                    DataValidated = True
                End If
            End If
        End If

        If DataValidated Then
            TableName = cboTableName.Text
            DisplayColumn = cboDisplayColumn.Text
            ReturnColumn = cboReturnColumn.Text
            OkClicked = True
        Else
            ' clear exiting grid cells.
            TableName = ""
            DisplayColumn = ""
            ReturnColumn = ""
            OkClicked = True
        End If


        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub frmGetLookupTableSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub


    Private mDatabaseName As String = ""
    Private mCS As String = ""

    Public Sub New(ByVal CS As String, ByVal DatabaseName As String)
        mDatabaseName = DatabaseName
        mCS = CS
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Dim Connection As New SqlConnection(CS)
        Connection.Open()

        Dim sql As String = "select * from [" & DatabaseName & "].dbo.sysobjects where xtype = 'U' order by name"
        Dim Command As New SqlCommand(sql, Connection)
        cboTableName.Items.Add("")
        Dim Reader As SqlDataReader = Command.ExecuteReader
        Dim dt As New DataTable
        dt.Columns.Add("Key", GetType(String))
        dt.Columns.Add("Value", GetType(String))

        Dim dr As DataRow = dt.NewRow
        dr("Key") = ""
        dr("Value") = ""
        dt.Rows.Add(dr)

        Do While Reader.Read

            dr = dt.NewRow
            dr("Key") = Reader("ID")
            dr("Value") = Reader("Name")
            dt.Rows.Add(dr)
            'cboTableName.Items.Add(Reader("Name"))
        Loop
        cboTableName.ValueMember = "Key"
        cboTableName.DisplayMember = "Value"
        cboTableName.DataSource = dt
        Reader.Close()
        'DataGridView1.DataSource = Command.ExecuteReader

        Connection.Close()

    End Sub

    Private Sub LoadClassGenerationCodingStyle()

    End Sub

    Public Sub cboTableName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTableName.SelectedIndexChanged
        cboDisplayColumn.Items.Clear()
        cboReturnColumn.Items.Clear()
        If cboTableName.SelectedValue <> "" Then
            Dim Connection As New SqlConnection(mCS)
            Connection.Open()
            Dim Command As New SqlCommand("select * from " & mDatabaseName & "..syscolumns where id = " & cboTableName.SelectedValue & " order by [Name]", Connection)

            Dim Reader As SqlDataReader = Command.ExecuteReader
            Do While Reader.Read
                cboDisplayColumn.Items.Add(Reader("Name"))
                cboReturnColumn.Items.Add(Reader("Name"))
            Loop
            Reader.Close()
            Connection.Close()
        End If

    End Sub
End Class
